package com.zijie.members.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zijie.core.util.JsonResult;
import com.zijie.members.model.Members;
import com.zijie.members.model.search.MemberSearchBean;
import com.zijie.members.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/member")
public class MembersApi {
    //依赖注入：
    private MemberService memberService;

    @Autowired
    public void setMemberService(MemberService memberService) {
        this.memberService = memberService;
    }

    //查询会员信息：
    @GetMapping
    public ResponseEntity<JsonResult<?>> findAll(@RequestParam(defaultValue = "1") Integer pageNo,
                                                 @RequestParam(defaultValue = "10") Integer pageSize,
                                                 @ModelAttribute MemberSearchBean memberSearchBean) {
        Page<Members> page = new Page<>(pageNo, pageSize);
        Page<Members> all = memberService.findAll(page, memberSearchBean);
        return ResponseEntity.ok(JsonResult.success(all));
    }

    //根据id单个的删除：
    @DeleteMapping("/deleteById/{id}")
    public ResponseEntity<JsonResult<?>> deleteById(@PathVariable Integer id) {
        return ResponseEntity.ok(JsonResult.success(memberService.deleteById(id)));
    }

    //添加会员 ：
    @PostMapping("/add")
    public ResponseEntity<JsonResult<?>> save(@RequestBody Members members) {
        return ResponseEntity.ok(JsonResult.success(memberService.save(members)));
    }

    //更新会员：
    @PutMapping("/update")
    public ResponseEntity<JsonResult<Members>> update(@RequestBody Members members) {
        boolean result = memberService.update(members);
        if (result) {
            // 更新成功后，查询并返回更新后的对象
            Members updatedMember = memberService.findById(members.getId());
            return ResponseEntity.ok(JsonResult.success(updatedMember));
        } else {
            return ResponseEntity.ok(JsonResult.error("500", "更新失败"));
        }
    }

    //批量删除：
    @DeleteMapping("/delete")
    public ResponseEntity<JsonResult<?>> deleteAll(@RequestParam("ids") List<Integer> ids) {
        return ResponseEntity.ok(JsonResult.success(memberService.deleteAll(ids)));
    }

    //更新会员的状态（启用 /禁用）
    @PutMapping("/updateEnabled")
    public ResponseEntity<JsonResult<?>> updateEnabled(@RequestParam("id") Integer id, @RequestParam("enabled") Boolean enabled) {
        return ResponseEntity.ok(JsonResult.success(memberService.updateEnabled(id, enabled)));
    }

    //根据id查询会员信息：
    @RequestMapping("/{id}")
    public ResponseEntity<JsonResult<?>> findById(@PathVariable Integer id) {
        return ResponseEntity.ok(JsonResult.success(memberService.findById(id)));
    }
    //根据用户查询
    @GetMapping("/findByPhone/{phone}")
    public ResponseEntity<JsonResult<?>> findByPhone(@PathVariable String phone) {
        return ResponseEntity.ok(JsonResult.success(memberService.findByPhone(phone)));
    }
}